package product;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProductInputList extends JInternalFrame {

	HashMap<String, ProductData> map;
	ArrayList<ProductData> list;

	DefaultTableModel model;

	Vector<String> v = new Vector<String>();

	private JPanel contentPane;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					ProductInputList frame = new ProductInputList();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */

	public void setD(HashMap map, ArrayList list) {
		this.map = map;
		this.list = list;
	}
	


	public ProductInputList() {
		super("ProductInputList",false,true,false,false);

		setBounds(100, 100, 515, 447);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());

		v.add("제품코드");
		v.add("제품명");
		v.add("단가");
		v.add("수량");
		v.add("생산날짜");

		model = new DefaultTableModel(v, 0) {
			@Override
			public boolean isCellEditable(int row, int column) { // 테이블에서 수정
																	// 안되게함~
				return false;
			}
		};

		contentPane.add(getScrollPane());
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("history");
			btnNewButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					//모델의 행수를 설정합니다
					model.setRowCount(0); 
					Iterator<ProductData> it = list.iterator();
					
					while(it.hasNext()){
						ProductData st = it.next();
						
						Vector<String> vv = new Vector<String>();
						
						vv.add(st.getCode());
						vv.add(st.getName());
						vv.add(st.getPrice()+"");
						vv.add(st.getVolume()+"");
						vv.add(st.getNal());
						
						model.addRow(vv);
					}
				}
			});
			btnNewButton.setBounds(12, 10, 97, 23);
		}
		return btnNewButton;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("누적생산량");
			btnNewButton_1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					//모델의 행수를 설정합니다
					model.setRowCount(0); 
					
					Set<String> st = map.keySet();
					Iterator<String> it = st.iterator();

					while (it.hasNext()) {
						String key = it.next();
						ProductData val = map.get(key);
						
						Vector<String> vv = new Vector<String>();
						
						vv.add(val.getCode());
						vv.add(val.getName());
						vv.add(val.getPrice()+"");
						vv.add(val.getVolume()+"");
						vv.add(val.getNal());
						
						model.addRow(vv);
					}
				}
			});
			btnNewButton_1.setBounds(121, 10, 108, 23);
		}
		return btnNewButton_1;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 55, 466, 325);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
		}
		return table;
	}
}
